package com.whzykj.ctrl;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.whzykj.common.DateUtil;
import com.whzykj.model.Notice;
import com.whzykj.model.Userinfo;

public class NoticeController extends Controller{
	public void index(){
		this.renderJsp("/admin/system/noticeList.jsp");
	}
	
	/**
	 * 显示所有公告信息
	 */
	public void listNotice(){
		Integer page = this.getParaToInt("page");
		Integer rows = this.getParaToInt("rows");
		StringBuffer condition=new StringBuffer("where 1=1 ");
		String[] statusList={"TO_DAYS(NOW())<TO_DAYS(nt.START_DATE) ","TO_DAYS(NOW())>=TO_DAYS(nt.START_DATE) AND TO_DAYS(NOW())<=TO_DAYS(nt.END_DATE) ","TO_DAYS(NOW())>TO_DAYS(nt.END_DATE) "};
		Integer status=this.getParaToInt("status");
		Integer kind=this.getParaToInt("kind");
		String username=this.getPara("username");
		String sqlBefore = "select nt.*,ui.USERNAME  ";
		if(status!=null && status!=-1){
			condition.append(" and "+statusList[status-1]);
		}
		if(kind!=null && kind!=-1){
			condition.append(" AND nt.NOTICE_KIND=3 ");
		}
		if(username!=null && username.trim().length()!=0){
			condition.append(" AND ui.USERNAME like'%"+username+"%' ");
		}
		StringBuffer sqlAfter = new StringBuffer(
				"from notice nt ");
		sqlAfter.append("LEFT JOIN userinfo ui on nt.USERID=ui.USERID ");
		sqlAfter.append(condition.toString());
		sqlAfter.append(" order by nt.START_DATE DESC ");
		Page<Notice> paginate = Notice.dao.paginate(page, rows, sqlBefore,
				sqlAfter.toString());
		Map map = new HashMap();
		map.put("rows", paginate.getList());
		map.put("total", paginate.getTotalRow());
		this.renderJson(map);	
	}
	
	/**
	 * 进入添加新公告页面
	 */
	public void addBefore(){
		this.renderJsp("/admin/system/noticeAdd.jsp");
	}
	
	/**
	 * 添加新的公告
	 */
	public void addNotice(){
		UploadFile upFile=getFile("img", "notice/");	
	//	文件重命名这样用：    getFile().getFile().renameTo(new File(newFileNameHere)); 
		if(upFile!=null){
			String fileName=upFile.getFileName();
			String imgUrl=PropKit.use("config.properties").get("web.url")+"/upload/notice/"+fileName;
			Notice notice=this.getModel(Notice.class);
			notice.set("CREATE_TIME", DateUtil.getNow());
			notice.set("TITLE_IMG_URL",imgUrl);
			Userinfo userinfo=(Userinfo)this.getSessionAttr("LOGIN_USER");
			notice.set("USERID", userinfo.getInt("USERID"));
			boolean add=notice.save();
			this.setAttr("addMsg", add?"公告信息添加成功！":"公告信息添加失败");
		}else{
			this.setAttr("addMsg","公告信息添加失败:没上传任何可用的标题图片");
			
		}		
		this.renderJsp("/admin/system/noticeAdd.jsp");
	}
	
	/**
	 * 进入编辑公告栏的页面
	 */
	public void editBefore(){
		Integer nid=this.getParaToInt(0);
		Notice notice=Notice.dao.findById(nid);
		this.setAttr("notice", notice);
		this.renderJsp("/admin/system/noticeEdit.jsp");	
	}
	
	/**
	 * 编辑公告信息
	 */
	public void editNotice(){
		UploadFile upFile=getFile("img", "notice/");	
		//	文件重命名这样用：    getFile().getFile().renameTo(new File(newFileNameHere)); 
		Notice notice=this.getModel(Notice.class);
		if(upFile!=null){
			String fileName=upFile.getFileName();
			String imgUrl=PropKit.use("config.properties").get("web.url")+"/upload/notice/"+fileName;		
			notice.set("TITLE_IMG_URL",imgUrl);
			
		}
		boolean add=notice.update();
		this.setAttr("addMsg", add?"公告信息修改成功！":"公告信息修改失败");
		this.renderJsp("/admin/system/noticeList.jsp");
	}
	
	/**
	 * 删除公告信息
	 */
	public void delNotice(){
		Integer nid=this.getParaToInt("noticeId");
		boolean del=Notice.dao.deleteById(nid);
		this.renderText(del?"1":"0");
	}
}
